<template>
  <div class="home">
    <div>
      <span @click="selectFrom">从:{{ from?from:"不限" }}</span>
      <span @click="switchCity">切换</span>
      <span @click="selectTo">到:{{ to?to:"不限" }}</span>
    </div>

    <button @click="search">搜索</button>

    <div>
      <p>搜索结果</p>
       <div v-if="!search_ticket_list.length">
           没有搜到任何结果
       </div>

        <div v-if="search_ticket_list.length">
          <ul>
        <li v-for="(item ,index) in search_ticket_list" :key="index">
          {{item.date}} {{item.title}}<br>
         出发城市： {{item.from}}<br>
           到达城市： {{item.to}}<br>
          {{item.price}} 元<br>
          还剩：{{item.stock}}张
          
        </li>
      
      </ul>
       </div>
     
    </div>

  

    <div v-show="open" class="cityModel">
      <div class="cityBox">
        城市列表：
        <ul >
          <li @click="selectCity('')">不限</li>
          <li v-for="(item,index ) in city_list" :key="index" @click="selectCity(item.city)">{{item.city}}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState,mapMutations } from "vuex";

export default {
  data() {
    return {
      from: "",
      to: "",
      select: "from",
      open: false,
    };
  },
  name: "Home",
  components: {},
  created() {
    this.getCityList();
    this.getTicketList();
  },
  computed:{
   ...mapState(['city_list','search_ticket_list'])
  },
  methods: {
    ...mapActions(["getCityList", "getTicketList"]),
    ...mapMutations(["SEARCH_TICKET"]),

    // 切换城市
    switchCity() {
      let toCity = this.to;
      this.to = this.from;
      this.from = toCity;
    },

    // 选择from 城市
    selectFrom() {
      this.open = true;  //弹框显示
      this.select = "from";
    },

    // 选择to 城市
    selectTo() {
      this.open = true;  //弹框显示
      this.select = "to";
    },

    // 选择城市
    selectCity(city){
      this.open=false;  // 弹框消失
      if(this.select==='from'){
        this.from=city;
      }else{
        this.to=city;
      }

    },

    // 搜索
    search(){ 
       this.SEARCH_TICKET({from:this.from,to:this.to})
    }
  },
};
</script>
<style scoped>
.cityModel {
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: 100%;
  background: rgba(0, 0, 0, 0.5);
}
.cityBox {
  position: relative;
  height: 100vh;
  width: 90%;
  margin: 0 auto;
  background: white;
}
</style>